package com.nt.oam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nt.common.core.domain.entity.SysRole;
import com.nt.common.core.domain.entity.SysUser;
import com.nt.common.enums.YesOrNoEnum;
import com.nt.common.utils.DateUtils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import com.nt.common.utils.SecurityUtils;
import com.nt.common.utils.StringUtils;
import com.nt.oam.domain.AntDatabaseAsset;
import com.nt.oam.domain.AntHospital;
import com.nt.system.service.impl.SysRoleServiceImpl;
import com.nt.system.service.impl.SysUserServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.nt.oam.mapper.AntPersonnelManageMapper;
import com.nt.oam.domain.AntPersonnelManage;
import com.nt.oam.service.IAntPersonnelManageService;

/**
 * 人员管理Service业务层处理
 *
 * @author JZFeng
 * @date 2025-02-05
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AntPersonnelManageServiceImpl extends ServiceImpl<AntPersonnelManageMapper, AntPersonnelManage> implements IAntPersonnelManageService {
    private final AntPersonnelManageMapper antPersonnelManageMapper;
    private final SysRoleServiceImpl sysRoleService;
    private final SysUserServiceImpl sysUserService;

    private final AntHospitalServiceImpl antHospitalService;

    /**
     * 查询人员管理
     *
     * @param personId 人员管理主键
     * @return 人员管理
     */
    @Override
    public AntPersonnelManage selectAntPersonnelManageByPersonId(Long personId) {
        return antPersonnelManageMapper.selectAntPersonnelManageByPersonId(personId);
    }

    /**
     * 查询人员管理列表
     *
     * @param antPersonnelManage 人员管理
     * @return 人员管理
     */
    @Override
    public List<AntPersonnelManage> selectAntPersonnelManageList(AntPersonnelManage antPersonnelManage) {
        return antPersonnelManageMapper.selectAntPersonnelManageList(antPersonnelManage);
    }

    /**
     * 新增人员管理
     *
     * @param antPersonnelManage 人员管理
     * @return 结果
     */
    @Override
    public int insertAntPersonnelManage(AntPersonnelManage antPersonnelManage) {
        antPersonnelManage.setCreateTime(DateUtils.getNowDate());
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antPersonnelManage.setCreatePerson(sysUser.getUserName());
        }
        antPersonnelManage.setIsDeleted(YesOrNoEnum.NO.getCode());
        antPersonnelManage.setIsModify(YesOrNoEnum.NO.getCode());
        // 查询医院id
        if(StringUtils.isNotEmpty(antPersonnelManage.getPersonOrganization())) {
            LambdaQueryWrapper<AntHospital> antHospitalLambdaQueryWrapper = new LambdaQueryWrapper<>();
            antHospitalLambdaQueryWrapper.eq(AntHospital::getHospitalName, antPersonnelManage.getPersonOrganization());
            antHospitalLambdaQueryWrapper.eq(AntHospital::getIsDeleted,YesOrNoEnum.NO.getCode());
            antHospitalLambdaQueryWrapper.last("limit 1");
            AntHospital antHospital = antHospitalService.getOne(antHospitalLambdaQueryWrapper);
            if(Objects.nonNull(antHospital) && Objects.nonNull(antHospital.getHospitalId())){
                antPersonnelManage.setPersonOrganizationId(antHospital.getHospitalId());
            }
        }
        return antPersonnelManageMapper.insertAntPersonnelManage(antPersonnelManage);
    }

    /**
     * 修改人员管理
     *
     * @param antPersonnelManage 人员管理
     * @return 结果
     */
    @Override
    public int updateAntPersonnelManage(AntPersonnelManage antPersonnelManage) {
        antPersonnelManage.setUpdateTime(DateUtils.getNowDate());
        // 更新人
        // 创建人
        Long userId = SecurityUtils.getUserId();
        SysUser sysUser = sysUserService.selectUserById(userId);
        if (Objects.nonNull(sysUser) && StringUtils.isNotEmpty(sysUser.getUserName())) {
            antPersonnelManage.setUpdatePerson(sysUser.getUserName());
        }
        // 查询医院id
        if(StringUtils.isNotEmpty(antPersonnelManage.getPersonOrganization())) {
            LambdaQueryWrapper<AntHospital> antHospitalLambdaQueryWrapper = new LambdaQueryWrapper<>();
            antHospitalLambdaQueryWrapper.eq(AntHospital::getHospitalName, antPersonnelManage.getPersonOrganization());
            antHospitalLambdaQueryWrapper.eq(AntHospital::getIsDeleted,YesOrNoEnum.NO.getCode());
            antHospitalLambdaQueryWrapper.last("limit 1");
            AntHospital antHospital = antHospitalService.getOne(antHospitalLambdaQueryWrapper);
            if(Objects.nonNull(antHospital) && Objects.nonNull(antHospital.getHospitalId())){
                antPersonnelManage.setPersonOrganizationId(antHospital.getHospitalId());
            }
        }
        return antPersonnelManageMapper.updateAntPersonnelManage(antPersonnelManage);
    }

    /**
     * 批量删除人员管理
     *
     * @param personIds 需要删除的人员管理主键
     * @return 结果
     */
    @Override
    public int deleteAntPersonnelManageByPersonIds(Long[] personIds) {
        List<Long>  personIdsList = Arrays.asList(personIds);
        personIdsList.forEach(a -> {
            AntPersonnelManage antPersonnelManage = antPersonnelManageMapper.selectAntPersonnelManageByPersonId(a);
            antPersonnelManage.setIsDeleted(YesOrNoEnum.YES.getCode());
            antPersonnelManageMapper.updateAntPersonnelManage(antPersonnelManage);
        });
        //antPersonnelManageMapper.deleteAntPersonnelManageByPersonIds(personIds);
        return YesOrNoEnum.YES.getCode();
    }

    /**
     * 删除人员管理信息
     *
     * @param personId 人员管理主键
     * @return 结果
     */
    @Override
    public int deleteAntPersonnelManageByPersonId(Long personId) {
        return antPersonnelManageMapper.deleteAntPersonnelManageByPersonId(personId);
    }


    /**
     * 查询是否在修改
     */
    public int selectPersonModify(Long personId) {
        AntPersonnelManage antPersonnelManage = this.getById(personId);
        int isModify = 0;
        if (Objects.nonNull(antPersonnelManage) && Objects.nonNull(antPersonnelManage.getIsModify())) {
            isModify = antPersonnelManage.getIsModify();
        }
        return isModify;
    }

    /**
     * 重新设置修改状态
     */
    public int updatePersonModify(Long personId, Integer isModify) {
        AntPersonnelManage antPersonnelManage = this.getById(personId);
        if (Objects.nonNull(antPersonnelManage)) {
            antPersonnelManage.setIsModify(isModify);
        }
        return antPersonnelManageMapper.updateAntPersonnelManage(antPersonnelManage);
    }
}
